﻿//全局变量
var table, myGrid, ised = "";




function Test() {
    var rows = document.getElementById("dt_log").rows;
    //alert(123)
    if (rows.length > 0) {
        for (var i = 1; i < rows.length; i++) {
            (function (i) {
                var temp = rows[i].cells[0].childNodes[1].value;
                var obj = rows[i];
                obj.ondblclick = function () { alert(temp); };
            })(i)
        }
    }
}
function getTdValue() {
    $($("table tr:last td")[0]).removeClass("select-checkbox");
}
function checkall() {
    $('.checkall').is(':checked') ? $('.table-responsive tbody tr').addClass('selected') : $('.table-responsive tbody tr').removeClass('selected');
}



$(document).on('ready', function () {
    $("#txt_file").fileinput({ showCaption: false });
});

var Btn_Import = function () {
    $('#ImportModal').modal('show');
}
$(document).ready(function () {


    GetMenuButtonList('btnOp');
    //#region datatable初始化及值绑定
    $.extend($.fn.dataTable.defaults, {
        dom: 'rt<"#dt-bottom.row"<"col-xs-2"l><"col-xs-7"p><"col-xs-3"i>>'
    });
    var scrollY = document.body.clientHeight - 56 - 53 - 38 - 32 - 13 - 4;
    //适应窗口大小变化
    $(window).resize(function () {
        var newScrollY = document.body.clientHeight - 56 - 53 - 38 - 32 - 13 - 4;
        $('.dataTables_scrollBody').css('height', newScrollY);
    });
    myGrid = {
        id: 'dt_admin',
        isenable: false,//是否需要禁用启用按钮
        scrollY: scrollY,
        scrollX: true,
        paging: true,
        hasIndex: false,
        multiSelect: true,
        processing: true,
        serverSide: true,
        columns: [
            { data: 'option3', title: " <input type='checkbox' class='checkall' onchange='checkall()' />", bSortable: false },
            { data: "Row", title: "#" },
            { data: "name", title: "会员名称" },//"visible": false 
            { data: "mobile", title: "电话" },
            { data: "card", title: "身份证号" },
            { data: "cardno", title: "卡号" },
            { data: 'dealResult', title: '状态' }
        ],
        columnDefs: [
            {
                orderable: false,
                className: 'select-checkbox',
                targets: [0]
            },
            {
                render: function (data, type, row) {
                    if (data == 0) {
                        return "待确认";
                    }
                    if (data == 1) {
                        return "已通过";
                    } else {
                        return "已拒绝";
                    }
                },
                targets: [6]
            }],
        order: [[2, 'asc']],
        data: {},
        ajax: function (data, callback, settings) {
            //封装请求参数
            var reUrl = 'GetUpGrades';
            var order = data.columns[data.order[0].column].data + " " + data.order[0].dir;
            var pagesize = data.length;
            var pageno = (data.start / data.length) + 1;
            //查询条件
            var searchData = { pagesize: pagesize, pageno: pageno, order: order, query: $('#query').val(), state: $('#carstate').val() };
            //ajax请求数据
            $.ajax({
                type: "GET",
                url: reUrl,
                cache: false,//禁用缓存
                data: searchData,//传入组装的参数
                dataType: "json",
                //headers: param,//请求头参数
                success: function (result, status, xhr) {
                    //封装返回数据
                    var returnData = {};
                    returnData.draw = data.draw; //这里直接自行返回了draw计数器,应该由后台返回
                    returnData.recordsTotal = result.TotalCount;
                    total_curr = result.TotalCurr;
                    returnData.recordsFiltered = result.TotalCount; //后台不实现过滤功能，每次查询均视作全部结果
                    returnData.data = result.Entity; //返回的数据列表
                    //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染
                    //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕
                    callback(returnData);//将你的返回的data转为datatables能识别的数据格式作为参数返回ajax的callback回调中。
                }
            });
        }
    }
    table = DataBind(myGrid);
    $('#datetimepicker_start').datetimepicker({
        format: 'YYYY-MM-DD'
    });
    $('#btnInquire').click(function () {
        loadTableData();
    })
});
var Btn_Export = function () {
    if (table.rows('.selected').data().length == 0) {
        $.notify({
            message: '<i class="fa fa-bell-o"></i>请选择需要导出的数据！',
            status: "info",
            group: null,
            pos: "bottom-right"
        });
        return;
    }
    var ids = [];
    for (var i = 0; i < table.rows('.selected').data().length; i++) {
        ids.push(table.rows('.selected').data()[i].id)
    }
    $.ajax({
        url: 'ToExcel',
        data: { ids: ids.join() },
        type:"POST",
        success: function (result) {
            window.location.href = "../../Out/" + result;
        }
    })
}
var Btn_Pass = function () {
    PostToServer(1);
}
var Btn_Fail = function () {
    PostToServer(2);
}
function PostToServer(type) {
    if (table.rows('.selected').data().length == 0) {
        $.notify({
            message: '<i class="fa fa-bell-o"></i> 请选择需要操作的数据！',
            status: "info",
            group: null,
            pos: "bottom-right"
        });
        return;
    }
    var ids = [];
    for (var i = 0; i < table.rows('.selected').data().length; i++) {
        ids.push(table.rows('.selected').data()[i].id)
    }
    $.ajax({
        url: "PassOrNoPass",
        data: { "ids": ids.join(), "state": type },
        method: 'POST',
        succes: function (result) {
            result = eval('(' + result + ')');
            $.notify({
                message: '<i class="fa ' + (result.IsSucceed ? 'fa-check' : 'fa-exclamation') + '"></i> ' + result.Message,
                status: result.IsSucceed ? 'success' : "danger",
                pos: "bottom-right"
            });
            if (result.IsSucceed) {
                loadTableData(myGrid);
            }
            else {
                if (result.Entity != "" && result.Entity != null)
                    window.parent.location.href = result.Entity;
            }
        }
    });
}

//刷新datatable
function loadTableData() {
    table.ajax.reload();
}

var Btn_Down = function () {
    window.location.href = "../../template/升级管理激活请求模板.xlsx";
}

//删除
var Btn_Delete = function () {
    if (table.rows('.selected').data().length == 0) {
        $.notify({
            message: '<i class="fa fa-bell-o"></i> 请选择要删除的数据！',
            status: "info",
            group: null,
            pos: "bottom-right"
        });
        return;
    }
    bconfirm("确定删除？", LogDelete);
}
//删除方法
function LogDelete() {
    var ids = [];
    for (var i = 0; i < table.rows('.selected').data().length; i++) {
        ids.push(table.rows('.selected').data()[i].id);
    }
    $.ajax({
        type: "post",
        contentType: "application/json;utf-8",
        url: "DeleteCar?ids=" + ids.join() + "&t=" + new Date().getTime(),
        data: "",
        success: function (result) {
            result = eval('(' + result + ')');
            $.notify({
                message: '<i class="fa ' + (result.IsSucceed ? 'fa-check' : 'fa-exclamation') + '"></i> ' + result.Message,
                status: result.IsSucceed ? 'success' : "danger",
                pos: "bottom-right"
            });
            if (result.IsSucceed) {
                loadTableData(myGrid);
            }
            else {
                if (result.Entity != "" && result.Entity != null)
                    window.parent.location.href = result.Entity;
            }
        }
    });
}
